<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SigningController extends Controller
{

    protected $openId;

    protected $appSecret;
    protected $baseUrl;
    protected $uUid;

    public function __construct()
    {
        //初始配置

        //公用配置
        $this->openId = env('OPEN_ID');
        $this->appSecret = env('APP_SECRET');
        //测试环境
        $this->baseUrl = env('BASE_URL');

        //随机生成nonce
        $chars = md5(uniqid(mt_rand(), true));
        $uuid = substr($chars, 0, 8) . '-';
        $uuid .= substr($chars, 8, 4) . '-';
        $uuid .= substr($chars, 12, 4) . '-';
        $uuid .= substr($chars, 16, 4) . '-';
        $uuid .= substr($chars, 20, 12);
        $this->uUid = $uuid;
    }

    //根据模板合同创建
    public function contractTpl(Request $request)
    {

        $url = $this->baseUrl . '/contract/from/tpl';

        $projectSn = 'PM' . mt_rand(uniqid());
        //参与加密
        $posts = [
            'openId' =>   $this->openId,
            'version' => '1.0',
            'nonce' => $this->uUid,
            'projectSn' => $projectSn,
            'files' => [
                ['sn' => 'sn15204922931'], //如多文件 多sn都需要参与加密
//        ['sn' => 'sn1520492210'],
            ]
        ];
        ksort($posts);

        $posts['sign'] = sha1(http_build_query($posts) . "&nonce=" . $posts['nonce'] . "&appSecret=" . $this->appSecret);
        /* var_dump($posts['nonce']);
        var_dump($posts['sign']);
        exit; */
//非加密部分
        /* print_r($posts);
        exit; */
        $posts['projectName'] = '拍卖合同' . date('mdHis');
        $posts['notifyUrl'] = 'https://www.notify_update.com';
//第1个合同


        $posts['files'][0]['name'] = '合同名称';
        $posts['files'][0]['templateId'] = 'HQ75975829';//须在和签平台首先创建模板，得到此模板ID
        $posts['files'][0]['permission'] = '*';
        $posts['files'][0]['params'] = json_encode(array('j' => '张四', 'y' => '张四'));//如无可写空json  '{}'

        $posts['files'][0]['sign'][0]['idCardType'] = '2';
        $posts['files'][0]['sign'][0]['idCardNo'] = '500233199204174111';
        $posts['files'][0]['sign'][0]['mobile'] = '18203007008';
        $posts['files'][0]['sign'][0]['name'] = '卢小艳';
        $posts['files'][0]['sign'][0]['identity'] = '1';
        $posts['files'][0]['sign'][0]['auto'] = 1;
        $posts['files'][0]['sign'][0]['reason'] = 'sn' . time();
        $posts['files'][0]['sign'][0]['location'] = 'sn' . time();


        $posts['files'][0]['sign'][1]['idCardType'] = '2';
        $posts['files'][0]['sign'][1]['idCardNo'] = '500233199201093111';
        $posts['files'][0]['sign'][1]['mobile'] = '18203007009';
        $posts['files'][0]['sign'][1]['name'] = '皮华桥';
        $posts['files'][0]['sign'][1]['identity'] = '2';
        $posts['files'][0]['sign'][1]['auto'] = 1;
        $posts['files'][0]['sign'][1]['reason'] = 'sn' . time();
        $posts['files'][0]['sign'][1]['location'] = 'sn' . time();


        $posts['files'][0]['sign'][1]['openId'] = 'b6e768656923edd23ff3b70e052f9aea12fedd23'; //openId 和 auth 只需要传一个
        /* $posts['files'][0]['sign'][1]['auto'] = 1;
        $posts['files'][0]['sign'][1]['identity'] = '2'; */
        $result = $this->exec_post($url, $posts);
        //$posts['files'][0]['sign'][1]['reason'] = 'sn'.time();//非必填
        //$posts['files'][0]['sign'][1]['stamp'] = 'invisible';//非必填
        //$posts['files'][0]['sign'][1]['location'] = 'sn'.time();//非必填


//第2个合同 如多文件 将以上file 0字段重复
//$posts['files'][1]['name'] …………

    }

    //公用方法
    function exec_post($url, $posts)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($posts));

        $result = curl_exec($ch);
        if (!$result) {
            var_dump(curl_error($ch));
        }
        curl_close($ch);
        return json_decode($result, true);
    }



}
